home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15206 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  132 lines

  1. Path: hobbes.cc.uga.edu!news
  2. From: Andy Dustman <andy@CCMSD.chem.uga.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Template troubles with partial solution (?)
  5. Date: Wed, 03 Apr 1996 23:49:55 -0500
  6. Organization: University of Georgia, Athens
  7. Message-ID: <316354F3.7DEA1F8@CCMSD.chem.uga.edu>
  8. NNTP-Posting-Host: neptune.chem.uga.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.3.80 i586)
  13. CC: andy@ccmsd.chem.uga.edu
  14.  
  15. I'm trying to modify an existing library of matrix routines into a template
  16. and I'm having lots of trouble. I'm using G++ 2.7.0, but 2.7.2 seems to have
  17. the same problems.
  18.  
  19. The original looks something like this:
  20.  
  21. matrix.h:
  22.  
  23. class matrix {
  24.     double* mat;
  25.     ...
  26.     matrix& operator+= (const matrix& m) ;
  27.     ...
  28. } ;
  29. ...
  30. matrix operator+ (const matrix& m, const matrix& n);
  31.  
  32. *********
  33. matrix.c:
  34.  
  35. #include "matrix.h"
  36.  
  37. matrix& matrix::operator+= (const matrix& m)
  38. { ... }
  39.  
  40. matrix operator + (const matrix& m, const matrix& n)
  41. {
  42.         matrix p = m;
  43.         p += n;
  44.         return p;
  45. }
  46.  
  47.  
  48. This compiles and works well. However, I would like to turn it into a
  49. template so I could use matrix<double> or matrix<complex>. So: I did
  50. s/double/T/ and s/matrix/matrix<T>/ throughout the source tree, plus some
  51. other editing and come up with this:
  52.  
  53. matrix.h:
  54.  
  55. template<class T>
  56. class matrix {
  57.     T* mat;
  58.     ...
  59.     matrix<T>& operator+= (const matrix<T>& m);
  60.     ...
  61. } ;
  62. template<class T> matrix<T> operator+ (const matrix<T>& m, const matrix<T>& n); \\ A
  63.  
  64. *********
  65. matrix.c:
  66.  
  67. #include "matrix.h"
  68.  
  69. template<class T> matrix<T> matrix<T>::operator+= (const matrix<T>& m);
  70. { ... }
  71.  
  72. template<class T> matrix<T> operator + (const matrix& m, const matrix& n) \\ B
  73. {
  74.         matrix<T> p = m;
  75.         p += n;
  76.         return p;
  77. }
  78.  
  79. **********
  80. dmatrix.c:
  81.  
  82. #include "matrix.c"
  83.  
  84. template class matrix<double>;
  85.  
  86.  
  87. This compiles, but at link time, I get:
  88.  
  89. test.o(.text+0x14d): undefined reference to `operator+(matrix<double> const &, matrix<double> const &)'
  90.  
  91. Okay, I figure, I'll just prepend "matrix<T>::" to "operator +" on line B
  92. above and that'll fix it, right? Uh-uh:
  93.  
  94. matrix.h:(end of template): method `operator +' not found in class `matrix<double>'
  95. matrix.c:B: `matrix<...>::operator +(matrix<...> &, matrix<...> &)' must take either zero or one argument
  96.  
  97. Now this is a pretty strange error, so I figure the obvious thing is to
  98. move line A up into the template and modify it to look like the += def:
  99.  
  100.     matrix<T>& operator+= (const matrix<T>& m);
  101.     matrix<T> operator+ (const matrix<T>& m, const matrix<T>& n);
  102.  
  103. I try this. It still insists on "either zero or one argument". So I oblige.
  104.  
  105.     matrix<T> operator+ (const matrix<T>& n);
  106.  
  107. and change matrix.c:
  108.  
  109. template<class T> matrix<T> operator + (const matrix& n) \\ B
  110. {
  111.         matrix<T> p = *this;
  112.         p += n;
  113.         return p;
  114. }
  115.  
  116. This compiles, and it seems to resolve the symbol problems. (I actually
  117. have quite a few more to fix, but I gotta go home!) (Yes, I worked this
  118. last part out writing this!) But the question remains:
  119.  
  120. In the original version, the operator+ is declared outside of the class
  121. definition, but in the template version, it seems to have to be declared
  122. within the class definition. Is this really how things should work, or
  123. did I miss something? If you don't know by now, I am just learning C++.
  124. I've got Stroustrup's "C++ PL, 2nd ed., reprint April 95", so any references
  125. to that would be useful. No, I haven't read the whole thing yet...
  126.  
  127. -- 
  128. Andy Dustman / Computational Center for Molecular Structure and Design / UGA
  129. ===== For PGP public key:  finger andy@neptune.chem.uga.edu | pgp -fka =====
  130. Sure, the Telecomm Act will create jobs: 100,000 new thought-cops on the net
  131. http://charon.chem.uga.edu/~andy    mailto:andy@CCMSD.chem.uga.edu    <}+++<
  132.